package com.cn.jinl.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cn.jinl.api.*;
import com.cn.jinl.conf.Config;
import com.cn.jinl.dao.OrderMapper;
import com.cn.jinl.dao.OrderOutInfoMapper;
import com.cn.jinl.dao.OrderOutRelateMapper;
import com.cn.jinl.dao.ProductStorageOutRelateMapper;
import com.cn.jinl.domain.*;
import com.cn.jinl.utils.*;
import com.cn.jinl.utils.msgSend.HttpClientUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@EnableScheduling
@Service
public class OrderOutInfoServiceImpl implements IOrderOutInfoService {

    @Autowired
    private OrderOutInfoMapper orderOutInfoMapper;

    @Autowired
    private OrderOutRelateMapper orderOutRelateMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private IProductStorageOutService productStorageOutService;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private ProductStorageOutRelateMapper productStorageOutRelateMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IOrderBackInfoService orderBackInfoService;

    @Autowired
    private IProcureStorageService procureStorageService;

    @Autowired
    Config config;

    @Override
    public List<OrderOutInfo> getOrderOutInfoList(QueryForm queryForm) throws Exception {
        //默认查询近三个月
        if (queryForm.getStartTime() == null || queryForm.getStartTime().isEmpty()) {
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            // 设置endTime为当前时间
            queryForm.setEndTime(sdf1.format(new Date()));
            // 设置startTime为三个月前
            calendar.add(Calendar.MONTH, -3);
            queryForm.setStartTime(sdf.format(calendar.getTime()));
        }
        int count = orderOutInfoMapper.selectOrderOutInfoListCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<OrderOutInfo> orderOutInfos = orderOutInfoMapper.selectOrderOutInfoList(queryForm);
        orderOutInfos.get(0).setCount(count);
        return orderOutInfos;
    }

    @Override
    public List<OrderOutInfo> getAPIOrderOutInfoList(QueryForm queryForm) throws Exception {
        OrderOutInfoExample example = new OrderOutInfoExample();
        example.createCriteria().andOrderOutStatusEqualTo("0").andStatusEqualTo(Constants.NORMAL_STATUS);
        example.setOrderByClause("create_time desc");
        List<OrderOutInfo>  orderOutInfos = orderOutInfoMapper.selectByExample(example);
        return orderOutInfos;
    }

    @Override
    public List<Order> getOrderOutRelateList(String id) throws Exception {
        if(StringUtils.isEmpty(id)){
            return Lists.newArrayList();
        }
        return orderMapper.selectOrderOutRelates(id);
    }

    @Override
    public List<Order> getOrderInfoByParam(QueryForm queryForm) throws Exception {
        List<Order> orderInfos = orderService.getOrderOutRelatesByParam(queryForm);
        if(!CollectionUtils.isEmpty(orderInfos)){
            List<String> orderIds = Lists.newArrayList();
            for(Order order : orderInfos){
                orderIds.add(order.getId());
            }
            ProductStorageOutRelateExample example = new ProductStorageOutRelateExample();
            example.createCriteria().andRelateIdIn(orderIds);
            List<ProductStorageOutRelate> relates = productStorageOutRelateMapper.selectByExample(example);

            Map<String,Integer> relateMap = Maps.newHashMap();
            for(ProductStorageOutRelate relate: relates){
                if(relateMap.containsKey(relate.getRelateId())){
                    int count = relateMap.get(relate.getRelateId());
                    relateMap.put(relate.getRelateId(),count+1);
                }else{
                    relateMap.put(relate.getRelateId(),1);
                }
            }
            for(Order order : orderInfos){
                if(relateMap.containsKey(order.getId())){
                    order.setStorageOutProductCount(relateMap.get(order.getId()));
                }
            }
        }
        return orderInfos;
    }

    @Override
    public Map<String, Integer> getSendOrderOutCount() throws Exception {
        OrderOutInfoExample example = new OrderOutInfoExample();
        example.createCriteria().andOrderOutStatusEqualTo("0").andStatusEqualTo(Constants.NORMAL_STATUS);
        int orderOutCount = orderOutInfoMapper.countByExample(example);
        int orderBackBSCount = orderBackInfoService.getAPIOrderBackInfoList(new QueryForm()).size();
        int orderBackingCount = orderBackInfoService.getOrderBackStorageList(new QueryForm()).size();
        int procureCount = procureStorageService.getProcureStorageWaitingCount();
        Map<String,Integer> maps = Maps.newHashMap();
        maps.put("orderOutCount",orderOutCount);
        maps.put("orderBackBSCount",orderBackBSCount);
        maps.put("orderBackingCount",orderBackingCount);
        maps.put("procureCount",procureCount);
        return maps;
    }

    @Override
    public String getOrderOutTip(String userId) {
        long size = redisTemplate.opsForList().size(userId);
        List<String> outCodes = orderOutInfoMapper.selectOrderOutInfoCode();
        String tip = "";
        if(size == 0){
            if(outCodes.size() == 0){
                return tip;
            }

            tip = "新增"+outCodes.size()+"发货任务！";
            redisTemplate.opsForList().leftPushAll(userId,outCodes);
            return tip;
        }
        List<String> orderOutCode = redisTemplate.opsForList().range(userId,0,size -1);
        Iterator<String> iterator = outCodes.iterator();
        while (iterator.hasNext()){
            String code = iterator.next();
            if(orderOutCode.contains(code)){
                iterator.remove();
            }
        }
        if(outCodes.size() > 0){
            tip = "新增"+outCodes.size()+"发货任务！";
            redisTemplate.opsForList().leftPushAll(userId,outCodes);
        }
        return tip;
    }

    @Override
    public List<OrderOutInfo> getTodayOrderOutInfoList() throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String timeStr = simpleDateFormat.format(new Date());
        String startTime = timeStr+" 00:00:00";
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat2.parse(startTime);
        List<String> statusList = Lists.newArrayList("0","1");
        OrderOutInfoExample example = new OrderOutInfoExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andOrderOutStatusIn(statusList);
        List<OrderOutInfo> orderOutInfos = orderOutInfoMapper.selectByExample(example);
        return orderOutInfos;
    }

    @Override
    public List<OrderRankingVo> getShippingDetailsList(QueryForm queryForm) throws Exception {
        //默认查询近三个月
//        if (queryForm.getStartTime() == null || queryForm.getStartTime().isEmpty()) {
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            Calendar calendar = Calendar.getInstance();
//            // 设置endTime为当前时间
//            queryForm.setEndTime(sdf.format(new Date()));
//            // 设置startTime为三个月前
//            calendar.add(Calendar.MONTH, -3);
//            queryForm.setStartTime(sdf.format(calendar.getTime()));
//        }
        int count = orderMapper.getShippingDetailsCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<OrderRankingVo> orders  = orderMapper.getShippingDetailsList(queryForm);
        orders.get(0).setCount(count);
        return orders;
    }

    @Override
    public void exportShippingDetail(QueryForm queryForm, HttpServletResponse response) throws Exception {
        Map<String, Object> result = new LinkedHashMap<>();
//        //默认查询近三个月
//        if (queryForm.getStartTime() == null || queryForm.getStartTime().isEmpty()) {
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//            Calendar calendar = Calendar.getInstance();
//            // 设置endTime为当前时间
//            queryForm.setEndTime(sdf.format(new Date()));
//            // 设置startTime为三个月前
//            calendar.add(Calendar.MONTH, -3);
//            queryForm.setStartTime(sdf.format(calendar.getTime()));
//        }
        List<OrderRankingVo> orders  = orderMapper.exportShippingDetail(queryForm);
        result.put("orders", orders);
        result.put("name", queryForm.getName());
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        result.put("startTime", simpleDateFormat.format(date));

        String fileName = CommonUtil.generateRandomNum("shippingDetail-")+".xlsx";
        String filePath = config.filePath+fileName;
        TemplateExcelUtils.downLoadExcel(filePath,"发货详情.xlsx",result,"shippingDetail_check.xlsx", response);
    }

    //获取order_out_info表中id为1的固定任务
    @Override
    public List<OrderOutInfo> getAPIOrderOutInfoListById(QueryForm queryForm) throws Exception {
        OrderOutInfoExample example = new OrderOutInfoExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andIdEqualTo("1");
        example.setOrderByClause("create_time desc");
        List<OrderOutInfo>  orderOutInfos = orderOutInfoMapper.selectByExample(example);
        return orderOutInfos;
    }

    @Override
    public OrderOutInfo getOrderOutInfoById(String id) throws Exception {
        OrderOutInfo orderOutInfo = orderOutInfoMapper.selectByPrimaryKey(id);
        return orderOutInfo;
    }

    @Override
    public String updateOrderOutInfo(OrderOutInfo orderOutInfo) throws Exception {
        String id = orderOutInfo.getId();
        if(StringUtils.isEmpty(id)){
            return "主键id为空";
        }
        int count = orderOutInfoMapper.updateByPrimaryKeySelective(orderOutInfo);
        return CommonUtil.outStr(count);
    }


    @Override
    public String disOrderOutInfo(OrderOutInfo orderOutInfo) throws Exception {
        String id = orderOutInfo.getId();
        if(StringUtils.isEmpty(id)){
            return "id 不能为空！";
        }
        OrderOutInfoExample example = new OrderOutInfoExample();
        example.createCriteria().andIdEqualTo(id);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String time = simpleDateFormat.format(new Date());
        OrderOutInfo update  = new OrderOutInfo();
        update.setOrderOutStatus("2");
        update.setDisUserTime(time);
        update.setDisUserName(orderOutInfo.getDisUserName());
        int count = orderOutInfoMapper.updateByExampleSelective(update,example);
        if(count > 0){
            List<String> orderIds = orderOutInfoMapper.selectOrderByOrderOutId(id);
            if(CollectionUtils.isEmpty(orderIds)){
                throw new Exception("未查询到订单");
            }
            Order updateOrder = new Order();
            updateOrder.setOrderStatus("9");
            OrderExample orderExample = new OrderExample();
            orderExample.createCriteria().andIdIn(orderIds);
            count = orderMapper.updateByExampleSelective(updateOrder,orderExample);
            if(count < 1){
                throw new Exception("更新订单异常！");
            }
        }
        return CommonUtil.outStr(count);
    }

    private List<OrderOutRelate> GetVoucherDTO(String outCode,Map<String,String> orderIdMap,String openToken){
        List<OrderOutRelate> orderOutRelates = Lists.newArrayList();
        String url = "https://openapi.chanjet.com/tplus/api/v2/SaleDeliveryOpenApi/GetVoucherDTO";
        JSONObject jsonObject = new JSONObject();

        JSONObject param = new JSONObject();
        param.put("voucherCode",outCode);
        jsonObject.put("param",param);
        String body = jsonObject.toJSONString();
        JSONObject header = new JSONObject();
        header.put("appKey",config.appKey);
        header.put("appSecret",config.appSecret);
        header.put("openToken",openToken);
        String result = HttpClientUtil.postJsonWithHeader(url, body, null,header);
        System.out.println(result);
        JSONObject respJson = JSON.parseObject(result);
        if(null == respJson){
            return orderOutRelates;
        }
        if(!StringUtils.equals("0",respJson.getString("code"))){
            return orderOutRelates;
        }
        JSONObject dataJson = respJson.getJSONObject("data");
        if(null == dataJson){
            return orderOutRelates;
        }
        JSONArray saleArray = dataJson.getJSONArray("SaleDeliveryDetails");
        if(null == saleArray || saleArray.size() == 0){
            return orderOutRelates;
        }

        for(int i= 0; i < saleArray.size(); i++){
            JSONObject saleJson = saleArray.getJSONObject(i);
            if(null == saleJson){
                continue;
            }
            Object detailMemo = saleJson.get("DetailMemo");
            if(null == detailMemo || StringUtils.isEmpty((CharSequence) detailMemo)){
                //过滤无用的
//                redisTemplate.opsForList().leftPush("outCodes",outCode);
                continue;
            }
            System.out.println("======detailMemo==========:"+detailMemo);
            if(!orderIdMap.containsKey(detailMemo)){
                //过滤无用的
//                redisTemplate.opsForList().leftPush("outCodes",outCode);
                System.out.println("detailMemo:"+detailMemo+" not found in our system:");
                continue;
            }
            JSONObject inventory = saleJson.getJSONObject("Inventory");
            if(null == inventory){
                continue;
            }
            String name = inventory.getString("Name");
            JSONArray array = saleJson.getJSONArray("DynamicPropertyValues");
            if(null == array || array.size()!= 3){
                continue;
            }
            String pieceNum = array.getString(1);
            OrderOutRelate orderOutRelate = new OrderOutRelate();
            orderOutRelate.setOrderId(orderIdMap.get(detailMemo));
            orderOutRelate.setGoodsName(name);
            orderOutRelate.setPieceNum(pieceNum);
            orderOutRelates.add(orderOutRelate);
        }
        return orderOutRelates;
    }

    //误差不超过10分钟
    @Scheduled(cron = "0 0/10 * * * ?")
    public void scanOrderOutInfo() throws Exception{
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        if(hour < 7 || hour > 20){
            return;
        }
        Object tokenObj = redisTemplate.opsForValue().get("tokenKey");
        if(null == tokenObj){
            return;
        }
        String openToken = tokenObj.toString();
//        String openToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJpc3YiLCJpc3MiOiJjaGFuamV0IiwidXNlcklkIjoiNjAwMDc4MjMzMjQiLCJvcmdJZCI6IjkwMDA3Nzk2ODk0IiwiYWNjZXNzX3Rva2VuIjoiYmstMzc0ZjBhOTEtMjZhZC00ZTBlLWE0NWEtZDAzZjMzYjVjNTE3IiwiYXVkIjoiaXN2IiwibmJmIjoxNzEzMDE1OTEwLCJhcHBJZCI6IjU4Iiwic2NvcGUiOiJhdXRoX2FsbCIsImFwcEtleSI6IkpET3RJQnJ3IiwiaWQiOiI2NGZmODliOC1lZjhjLTRkNzctODg4ZC1kZGFmYzExY2E1MGQiLCJleHAiOjE3MTM1MzQzMTAsImlhdCI6MTcxMzAxNTkxMCwib3JnQWNjb3VudCI6InVteDl0dXhvdHIxbSJ9.HefmObDTfkmglgpUS1x_VvUtFoyXdFyTn3WP0RDl1_8";
        String url = "https://openapi.chanjet.com/tplus/api/v2/SaleDeliveryOpenApi/FindVoucherList";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pageIndex",0);
        jsonObject.put("pageSize",100);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String nowStr = simpleDateFormat.format(new Date());
        String fiveDayBefore = simpleDateFormat.format(new Date().getTime() - 3600L*24L*5L*1000L);
        JSONObject paramDic = new JSONObject();
        JSONArray array = new JSONArray();
        JSONObject time = new JSONObject();
        time.put("text",fiveDayBefore);
        time.put("value",fiveDayBefore);

        JSONObject time2 = new JSONObject();
        time2.put("text",nowStr);
        time2.put("value",nowStr);
        array.add(time);
        array.add(time2);
        paramDic.put("SaleDelivery.VoucherDate",array);
        jsonObject.put("paramDic",paramDic);

        List<String> selectFields = Lists.newArrayList();
        selectFields.add("SaleDelivery.Code");
        selectFields.add("SaleDelivery.Customer.Name");
        selectFields.add("SaleDelivery.Address");
        selectFields.add("SaleDelivery.VoucherDate");
        selectFields.add("SaleDelivery.Clerk.Name");
        selectFields.add("SaleDelivery.Maker");
        jsonObject.put("selectFields",selectFields);
        String body = jsonObject.toJSONString();
        JSONObject header = new JSONObject();
        header.put("appKey",config.appKey);
        header.put("appSecret",config.appSecret);
        header.put("openToken",openToken);
        String result = HttpClientUtil.postJsonWithHeader(url, body, null,header);
        System.out.println("openToken========================"+openToken);
        System.out.println("Result========================"+result);
        if(StringUtils.isEmpty(result)){
            return;
        }
        JSONObject respJson = JSON.parseObject(result);
        if(!StringUtils.equals("0",respJson.getString("code"))){
            return;
        }
        JSONObject dataJson = respJson.getJSONObject("data");
        if(null == dataJson){
            return;
        }

        JSONArray rowArray = dataJson.getJSONArray("Rows");
        if(null == rowArray || rowArray.size() == 0){
            return;
        }

        List<String> outCodes = orderOutInfoMapper.selectOrderOutInfoCode();
        List<String> redisOutCodes = Lists.newArrayList();

        List<OrderOutInfo> orderOutInfos = Lists.newArrayList();
        long redisSize =  redisTemplate.opsForList().size("outCodes");
        if(redisSize > 0){
            redisOutCodes = redisTemplate.opsForList().range("outCodes",0,redisSize -1);
        }
        System.out.println("dingDanCode=========="+rowArray);
        for(int i = 0 ;i < rowArray.size() ;i++){
            JSONArray rows = rowArray.getJSONArray(i);
            String outCode = rows.getString(0);
            System.out.println(outCode);
            if(redisOutCodes.contains(outCode)){
                System.out.println("this code has query interface!");
                continue;
            }
            if(outCodes.contains(outCode)){
                System.out.println("db has exist this code!");
                continue;
            }
            OrderOutInfo orderOutInfo = new OrderOutInfo();
            orderOutInfo.setOrderOutCode(outCode);
            orderOutInfo.setCustomer( rows.getString(1));
            orderOutInfo.setCustomerAddress(rows.getString(2));
            orderOutInfo.setOrderOutTime(rows.getString(3));
            orderOutInfo.setDriver(rows.getString(4));
            orderOutInfo.setMakeOrderUser(rows.getString(5));
            orderOutInfo.setOrderOutStatus("0");
            orderOutInfos.add(orderOutInfo);

        }
        System.out.println("orderOutInfos=========="+orderOutInfos);
        if(orderOutInfos.size() == 0){
            return;
        }
        List<OrderOutRelate> batchAddRelates = Lists.newArrayList();
        Iterator<OrderOutInfo> iterator = orderOutInfos.iterator();
        Map<String,String> orderMap = orderService.getOrderIdMap();
        while (iterator.hasNext()){
            OrderOutInfo outInfo = iterator.next();
            String id = CommonUtil.generateRandomNum("outinof-");
            outInfo.setId(id);
            List<OrderOutRelate> orderOutRelates = this.GetVoucherDTO(outInfo.getOrderOutCode(),orderMap,openToken);
            if(CollectionUtils.isEmpty(orderOutRelates)){
                iterator.remove();
                continue;
            }
            for(OrderOutRelate relate : orderOutRelates){
                relate.setOrderOutId(id);
            }
            outInfo.setOrderCount(orderOutRelates.size());
            batchAddRelates.addAll(orderOutRelates);
        }
        if(orderOutInfos.size() == 0 ||batchAddRelates.size() == 0 ){
            return;
        }
        int count = orderOutInfoMapper.batchInsertOrderOutInfos(orderOutInfos);
        if(count < 1){
            return;
        }
        count = orderOutRelateMapper.batchInsertOrderOutRelates(batchAddRelates);
        if(count < 1){
            throw new Exception("插入表异常！");
        }
        String resp = productStorageOutService.addBatchProductStorageOut(orderOutInfos);
        if(!StringUtils.isEmpty(resp)){
            throw new Exception("添加入库信息异常！");
        }

        //redis添加已经查询的code
        for(int i = 0 ;i < orderOutInfos.size() ;i++){
            String outCode = orderOutInfos.get(i).getOrderOutCode();
            redisTemplate.opsForList().leftPush("outCodes",outCode);
        }
    }

    public static void main(String args[]){
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        System.out.println(hour);
        String url = "https://openapi.chanjet.com/v1/common/auth/selfBuiltApp/generateToken";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appTicket", "t-2f64cfb6f1194cea9103a087669d506a");
        jsonObject.put("certificate", "OXYwHSWAc22UPHxfIUM0STRXoa096Mk6cLbdXPDU1UoMXvCI9eCWdWLfKDBR2Q1IeqZtunUoHXaxxo+3u/hBsylld5DiAkQ3EX1qqxoyaE+AW880fuYBamDJZO/JgUqKG3anlbZjawp3FgNeDK/XGfQi3TAFcwbxtjdRYa8TQmM=");

        String body = jsonObject.toJSONString();
        JSONObject header = new JSONObject();
        header.put("appKey","JDOtIBrw");
        header.put("appSecret","EA6ECD43D1CA86ADA45F68F48C6729D0");
        String result = HttpClientUtil.postJsonWithHeader(url, body, null,header);
        System.out.println(result);
//        getOrderDetail();
    }

    private static  void getOrderList(){
        String url = "https://openapi.chanjet.com/tplus/api/v2/SaleDeliveryOpenApi/FindVoucherList";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pageIndex",0);
        jsonObject.put("pageSize",5);

        JSONObject paramDic = new JSONObject();
        JSONArray array = new JSONArray();
        JSONObject time = new JSONObject();
        time.put("text","2023-01-23");
        time.put("value","2023-01-23");

        JSONObject time2 = new JSONObject();
        time2.put("text","2023-02-23");
        time2.put("value","2023-02-23");
        array.add(time);
        array.add(time2);
        paramDic.put("SaleDelivery.VoucherDate",array);
        jsonObject.put("paramDic",paramDic);

        List<String> selectFields = Lists.newArrayList();
        selectFields.add("SaleDelivery.Customer.Name");
        selectFields.add("SaleDelivery.Code");
        selectFields.add("SaleDelivery.VoucherDate");
        selectFields.add("SaleDelivery.Address");
        selectFields.add("SaleDelivery.Clerk.Name");
        selectFields.add("SaleDelivery.Maker");
        jsonObject.put("selectFields",selectFields);
        String body = jsonObject.toJSONString();

        JSONObject header = new JSONObject();
        header.put("appKey","JDOtIBrw");
        header.put("openToken","eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJpc3YiLCJpc3MiOiJjaGFuamV0IiwidXNlcklkIjoiNjAwMDc4MjMzMjQiLCJvcmdJZCI6IjkwMDA3Nzk2ODk0IiwiYWNjZXNzX3Rva2VuIjoiYmstMzc0ZjBhOTEtMjZhZC00ZTBlLWE0NWEtZDAzZjMzYjVjNTE3IiwiYXVkIjoiaXN2IiwibmJmIjoxNzEwNTUyOTEwLCJhcHBJZCI6IjU4Iiwic2NvcGUiOiJhdXRoX2FsbCIsImFwcEtleSI6IkpET3RJQnJ3IiwiaWQiOiIyZDE1OTNiYS00NDYwLTRjYTMtOTNmMC00YzE2ZTYzNTk2MzAiLCJleHAiOjE3MTEwNzEzMTAsImlhdCI6MTcxMDU1MjkxMCwib3JnQWNjb3VudCI6InVteDl0dXhvdHIxbSJ9.awbwjjXZuykv0ORtgMfhvxmkbDeQ8UffRRKsavwgXjc");
        header.put("appSecret","EA6ECD43D1CA86ADA45F68F48C6729D0");
        String result = HttpClientUtil.postJsonWithHeader(url, body, null,header);
        System.out.println(result);

    }

    private static  void getOrderDetail(){
        String openToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJpc3YiLCJpc3MiOiJjaGFuamV0IiwidXNlcklkIjoiNjAwMDc4MjMzMjQiLCJvcmdJZCI6IjkwMDA3Nzk2ODk0IiwiYWNjZXNzX3Rva2VuIjoiYmstMzc0ZjBhOTEtMjZhZC00ZTBlLWE0NWEtZDAzZjMzYjVjNTE3IiwiYXVkIjoiaXN2IiwibmJmIjoxNzEwNTUyOTEwLCJhcHBJZCI6IjU4Iiwic2NvcGUiOiJhdXRoX2FsbCIsImFwcEtleSI6IkpET3RJQnJ3IiwiaWQiOiIyZDE1OTNiYS00NDYwLTRjYTMtOTNmMC00YzE2ZTYzNTk2MzAiLCJleHAiOjE3MTEwNzEzMTAsImlhdCI6MTcxMDU1MjkxMCwib3JnQWNjb3VudCI6InVteDl0dXhvdHIxbSJ9.awbwjjXZuykv0ORtgMfhvxmkbDeQ8UffRRKsavwgXjc";
        String url = "https://openapi.chanjet.com/tplus/api/v2/SaleDeliveryOpenApi/GetVoucherDTO";
        JSONObject jsonObject = new JSONObject();

        JSONObject param = new JSONObject();
        param.put("voucherCode","SA-2024-03-0046");
        jsonObject.put("param",param);
        String body = jsonObject.toJSONString();
        JSONObject header = new JSONObject();
        header.put("appKey","JDOtIBrw");
        header.put("openToken",openToken);
        header.put("appSecret","EA6ECD43D1CA86ADA45F68F48C6729D0");
        String result = HttpClientUtil.postJsonWithHeader(url, body, null,header);
        System.out.println(result);
    }
}
